<template>
  <div class="main">
    <div class="title">
      <a href="/login">登录</a>
      <span>·</span>
      <a class="active" href="/register">注册</a>
    </div>

    <div class="sign-up-container">
      <el-form ref="registerForm" :model="registerObj">

        <el-form-item class="input-prepend restyle" prop="nickname" :rules="[{ required: true, message: '请输入你的昵称', trigger: 'blur' }]">
          <div>
            <el-input type="text" placeholder="你的昵称" v-model="registerObj.nickname"/>
            <i class="iconfont icon-user"/>
          </div>
        </el-form-item>

        <el-form-item class="input-prepend restyle no-radius" prop="mobile" :rules="[{ required: true, message: '请输入手机号码', trigger: 'blur' },{validator: checkPhone, trigger: 'blur'}]">
          <div>
            <el-input type="text" placeholder="手机号" v-model="registerObj.mobile"/>
            <i class="iconfont icon-phone"/>
          </div>
        </el-form-item>

        <el-form-item class="input-prepend restyle no-radius" prop="code" :rules="[{ required: true, message: '请输入验证码', trigger: 'blur' }]">
          <div style="width: 100%;display: block;float: left;position: relative">
            <el-input type="text" placeholder="验证码" v-model="registerObj.code"/>
            <i class="iconfont icon-phone"/>
          </div>
          <div class="btn" style="position:absolute;right: 0;top: 6px;width: 40%;">
            <a v-if="sending" type="button" href="javascript:" @click="sendCode()" :value="codeText" style="border: none;background-color: none">{{codeText}}</a>
            <a v-else type="button" :value="codeText" style="border: none;background-color: none;color:#606266">{{codeText}}</a>
          </div>
        </el-form-item>

        <el-form-item class="input-prepend" prop="password" :rules="[{ required: true, message: '请输入密码', trigger: 'blur' }]">
          <div>
            <el-input type="password" placeholder="设置密码" v-model="registerObj.password"/>
            <i class="iconfont icon-password"/>
          </div>
        </el-form-item>

        <div class="btn">
          <input type="button" class="sign-up-button" value="注册" @click="register()">
        </div>
        <p class="sign-up-msg">
          点击 “注册” 即表示您同意并愿意遵守简书
          <br>
          <a target="_blank" href="http://www.jianshu.com/p/c44d171298ce">用户协议</a>
          和
          <a target="_blank" href="http://www.jianshu.com/p/2ov8x3">隐私政策</a> 。
        </p>
      </el-form>
      <!-- 更多注册方式 -->
      <div class="more-sign">
        <h6>社交帐号直接注册</h6>
        <ul>
          <li><a id="weixin" class="weixin" target="_blank" href="http://localhost:9001/api/ucenter/wx/login"><i
            class="iconfont icon-weixin"/></a></li>
          <li><a id="qq" class="qq" target="_blank" href="#"><i class="iconfont icon-qq"/></a></li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
  import '~/assets/css/sign.css'
  import '~/assets/css/iconfont.css'

  import registerApi from '@/api/register' // 导入注册API接口

  export default {
    layout: 'sign',                        // 使用登录注册布局
    data() {                               // 定义页面中显示的模型数据
      return {
        registerObj: {                     // 注册数据
          mobile: '',                      // --手机号
          code: '',                        // --验证码
          nickname: '',                    // --昵称
          password: ''                     // --密码
        },
        sending: true,                     // 是否可以发送验证码
        second: 60,                        // 倒计时间
        codeText: '获取验证码'              // 验证码显示文本
      }
    },
    methods: {
      
      /**
       * 会员注册
       */
      register() {
        // 表单验证
        this.$refs['registerForm'].validate((valid) => {
          // 验证成功
          if (valid) {
            const loading = this.$loading({
              lock: true,
              text: '注册中...',
              spinner: 'el-icon-loading',
              background: 'rgba(0, 0, 0, 0.5)'
            });
            registerApi.register(this.registerObj).then(response => {
              loading.close();
              // 提示信息
              this.$message.success('注册成功')
              // 跳转到登录页面
              this.$router.push({path:'/login'})
            }).catch(error => {
              console.error(error)
              loading.close();
            })
          }
        })
      },

      /**
       * 发送短信验证码
       */
      sendCode() {
        // 表单验证手机号
        this.$refs['registerForm'].validateField("mobile", errorMsg => {
          if (!errorMsg&&this.sending) {
            this.codeText = '正在发送...'
            registerApi.sendCode(this.registerObj.mobile).then(response => {
              this.sending = false
              // 调用倒计时的方法
              this.timeDown()
            })
          }
        })
      },

      /**
       * 倒计时
       */
      timeDown() {
        let result = setInterval(() => {
          --this.second;
          this.codeText = '重新发送 ' + this.second + ' 秒'
          if (this.second < 1) {
            clearInterval(result);
            this.sending = true;
            this.second = 60;
            this.codeText = "获取验证码"
          }
        }, 1000);
      },

      /**
       * 验证手机号
       */
      checkPhone (rule, value, callback) {
        if (!(/^1[34578]\d{9}$/.test(value))) {
          return callback(new Error('手机号码格式不正确'))
        }
        return callback()
      }
    }
  }
</script>
